package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.AttendanceTypeBo;
import org.eiahe.hr.attendance.domain.vo.AttendanceTypeVo;
import org.eiahe.hr.attendance.service.IAttendanceTypeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考勤人员类型管理控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/attendanceType")
public class AttendanceTypeController extends BaseController {

    private final IAttendanceTypeService attendanceTypeService;

    /**
     * 查询考勤人员类型列表
     */
    @SaCheckPermission("attendance:attendanceType:list")
    @GetMapping("/list")
    public TableDataInfo<AttendanceTypeVo> list(AttendanceTypeBo bo, PageQuery pageQuery) {
        return attendanceTypeService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取考勤人员类型详细信息
     */
    @SaCheckPermission("attendance:attendanceType:query")
    @GetMapping("/{typeId}")
    public R<AttendanceTypeVo> getInfo(@PathVariable Long typeId) {
        return R.ok(attendanceTypeService.queryById(typeId));
    }

    /**
     * 新增考勤人员类型
     */
    @SaCheckPermission("attendance:attendanceType:add")
    @Log(title = "新增考勤人员类型", linkType = "attendance", businessType = BusinessType.INSERT, nameField = "typeName")
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AttendanceTypeBo bo) {
        return toAjax(attendanceTypeService.insertByBo(bo));
    }

    /**
     * 修改考勤人员类型
     */
    @SaCheckPermission("attendance:attendanceType:edit")
    @Log(title = "修改考勤人员类型", linkType = "attendance", businessType = BusinessType.UPDATE, nameField = "typeName")
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AttendanceTypeBo bo) {
        return toAjax(attendanceTypeService.updateByBo(bo));
    }

    /**
     * 删除考勤人员类型
     */
    @SaCheckPermission("attendance:attendanceType:remove")
    @Log(title = "删除考勤人员类型", linkType = "attendance", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public R<Void> remove(@PathVariable Long[] typeIds) {
        return toAjax(attendanceTypeService.deleteWithValidByIds(List.of(typeIds)));
    }

    /**
     * 导出考勤人员类型列表
     */
    @SaCheckPermission("attendance:attendanceType:export")
    @Log(title = "导出考勤人员类型", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AttendanceTypeBo bo, HttpServletResponse response) {
        List<AttendanceTypeVo> list = attendanceTypeService.queryList(bo);
        ExcelUtil.exportExcel(list, "考勤人员类型数据", AttendanceTypeVo.class, response);
    }
}
